﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

using H2.NET.Business;
using H2.NET.Controls;

namespace H2.NET.Web.Pages
{
    public partial class UserChoose : TaskControl
    {
        #region 属性 Public
        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        private bool IsUsed
        {
            get
            {
                return ViewState["IsUsed"] == null ? false : (bool)ViewState["IsUsed"];
            }
            set { ViewState["IsUsed"] = value; }
        }
        #endregion

        public bool isReturned
        {
            get
            {
                return ViewState["isReturned"] == null ? false : (bool)ViewState["isReturned"];
            }
            set { ViewState["isReturned"] = value; }
        }

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前部门不需要显示，退出
            if (IsLoaded && !IsUsed || true)
            {
                this.Visible = false;
                return;
            }

            if (!Page.IsPostBack)
            {
                SetVisible(false , USR_Marketing_Add_Data_L, USR_Marketing_Add_Data_C, USR_Marketing_Add_Data);
                SetVisible(false, USR_Purchasing_Add_Data_L, USR_Purchasing_Add_Data_C, USR_Purchasing_Add_Data);
                SetVisible(false, USR_Logistic_Add_Data_L, USR_Logistic_Add_Data_C, USR_Logistic_Add_Data);
                SetVisible(false, USR_Finance_Run_Cost_L, USR_Finance_Run_Cost_C, USR_Finance_Run_Cost);
                SetVisible(false, USR_Production_PV_L, USR_Production_PV_C, USR_Production_PV);
                SetVisible(false, USR_Run_ICD_Price_L, USR_Run_ICD_Price_C, USR_Run_ICD_Price);
                SetVisible(false, USR_LAM_Run_Price_L, USR_LAM_Run_Price_C, USR_LAM_Run_Price);

                string Prestep = ((object[])TaskAPI["PreStep"])[0].ToString();

                if (!isReturned)
                {
                    IList<t_UserChoose> tUCs = (t_UserChoose.process == TaskAPI.Process & t_UserChoose.step == TaskAPI.Step).List<t_UserChoose>();
                    foreach (t_UserChoose tUC in tUCs)
                    {
                        if (tUC.UserChoose.ToLower() == ("Marketing_Add_Data").ToLower()) { SetVisible(true, USR_Marketing_Add_Data_L, USR_Marketing_Add_Data_C, USR_Marketing_Add_Data); }
                        if (tUC.UserChoose.ToLower() == ("Purchasing_Add_Data").ToLower()) { SetVisible(true, USR_Purchasing_Add_Data_L, USR_Purchasing_Add_Data_C, USR_Purchasing_Add_Data); }
                        if (tUC.UserChoose.ToLower() == ("Logistic_Add_Data").ToLower()) { SetVisible(true, USR_Logistic_Add_Data_L, USR_Logistic_Add_Data_C, USR_Logistic_Add_Data); }
                        if (tUC.UserChoose.ToLower() == ("Finance_Run_Cost").ToLower()) { SetVisible(true, USR_Finance_Run_Cost_L, USR_Finance_Run_Cost_C, USR_Finance_Run_Cost);}
                        if (tUC.UserChoose.ToLower() == ("Production_PV").ToLower()) { SetVisible(true, USR_Production_PV_L, USR_Production_PV_C, USR_Production_PV); }
                        if (tUC.UserChoose.ToLower() == ("Run_ICD_Price").ToLower()) { SetVisible(true, USR_Run_ICD_Price_L, USR_Run_ICD_Price_C, USR_Run_ICD_Price); }
                        if (tUC.UserChoose.ToLower() == ("LAM_Run_Price").ToLower()) { SetVisible(true, USR_LAM_Run_Price_L, USR_LAM_Run_Price_C, USR_LAM_Run_Price); }
                    }
                }

                this.IsUsed = USR_Finance_Run_Cost.Visible || USR_Production_PV.Visible || USR_LAM_Run_Price.Visible || USR_Run_ICD_Price.Visible || USR_Marketing_Add_Data.Visible || USR_Purchasing_Add_Data.Visible || USR_Logistic_Add_Data.Visible;
                this.Visible = IsUsed;
                this.IsLoaded = true;

                SetEnable(TaskAPI.IsAllowSubmit, USR_Finance_Run_Cost, USR_Production_PV, USR_LAM_Run_Price, USR_Run_ICD_Price, USR_Finance_Run_Cost_C, USR_Production_PV_C, USR_LAM_Run_Price_C, USR_Run_ICD_Price_C, USR_Marketing_Add_Data_C, USR_Purchasing_Add_Data_C, USR_Logistic_Add_Data_C);
                SetForeColor(Color.Blue, USR_Finance_Run_Cost_L, USR_Production_PV_L, USR_Run_ICD_Price_L, USR_LAM_Run_Price_L, USR_Marketing_Add_Data_L, USR_Purchasing_Add_Data_L, USR_Logistic_Add_Data_L);

                USR_Finance_Run_Cost_C.Checked = true;
                USR_Production_PV_C.Checked = true;
                USR_LAM_Run_Price_C.Checked = true;
                USR_Run_ICD_Price_C.Checked = true;
                USR_Marketing_Add_Data_C.Checked = false;
                USR_Purchasing_Add_Data_C.Checked = false;
                USR_Logistic_Add_Data_C.Checked = false;
            }
        }
        #endregion

        #region 事件 用户选择
        protected void USR_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string SQL = "select rtrim(NAME) as username, rtrim(FULLNAME) AS fullname from jobs where rtrim(name)='" + ((Dialog)sender).Text + "'";
                TaskDB.Log("", true, "UserChoose - USR_TextChanged:" + SQL);

                DBAPI.Storage("bpmDB");
                DataTable dt = DBAPI.Query(SQL);

                //根据查询结果进行赋值
                if (dt.Rows.Count > 0)
                {
                    ((Dialog)sender).Text = dt.Rows[0]["FULLNAME"].ToString();
                    switch (((Dialog)sender).ID)
                    {
                        case "USR_Finance_Run_Cost":
                            TaskAPI["Finance_Run_Cost"] = dt.Rows[0]["username"].ToString();
                            if (dt.Rows[0]["username"].ToString().Length > 0) { TaskAPI["_COST"] = "1"; } else { TaskAPI["_COST"] = "0"; }
                            break;
                        case "USR_Production_PV":
                            TaskAPI["Production_PV"] = dt.Rows[0]["username"].ToString();
                            if (dt.Rows[0]["username"].ToString().Length > 0) { TaskAPI["_PV"] = "1"; } else { TaskAPI["_PV"] = "0"; }
                            break;
                        case "USR_LAM_Run_Price":
                            TaskAPI["LAM_Run_Price"] = dt.Rows[0]["username"].ToString();
                            break;
                        case "USR_Run_ICD_Price":
                            TaskAPI["Run_ICD_Price"] = dt.Rows[0]["username"].ToString();
                            break;
                        case "USR_Marketing_Add_Data":
                            TaskAPI["Marketing_Add_Data"] = dt.Rows[0]["username"].ToString();
                            break;
                        case "USR_Purchasing_Add_Data":
                            TaskAPI["Purchasing_Add_Data"] = dt.Rows[0]["username"].ToString();
                            break;
                        case "USR_Logistic_Add_Data":
                            TaskAPI["Logistic_Add_Data"] = dt.Rows[0]["username"].ToString();
                            break;
                    }
                }
                else
                {
                    ((Dialog)sender).Text = "";
                    switch (((Dialog)sender).ID)
                    {
                        case "USR_Finance_Run_Cost":
                            TaskAPI["Finance_Run_Cost"] = "";
                            break;
                        case "USR_Production_PV":
                            TaskAPI["Production_PV"] = "";
                            break;
                        case "USR_LAM_Run_Price":
                            TaskAPI["LAM_Run_Price"] = "";
                            break;
                        case "USR_Run_ICD_Price":
                            TaskAPI["Run_ICD_Price"] = "";
                            break;
                        case "USR_Marketing_Add_Data":
                            //TaskAPI["Marketing_Add_Data"] = "";
                            break;
                        case "USR_Purchasing_Add_Data":
                            //TaskAPI["Purchasing_Add_Data"] = "";
                            break;
                        case "USR_Logistic_Add_Data":
                            //TaskAPI["Logistic_Add_Data"] = "";
                            break;
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region 事件 USR_CheckBox 选择
        protected void USR_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            switch (((Anthem.CheckBox)sender).ID)
            {
                case "USR_Finance_Run_Cost_C":
                    SetForeColor(USR_Finance_Run_Cost_C.Checked ? Color.Blue : Color.Black, USR_Finance_Run_Cost_L);
                    TaskAPI["Finance_Run_Cost"] = "";
                    break;

                case "USR_Production_PV_C":
                    SetForeColor(USR_Production_PV_C.Checked ? Color.Blue : Color.Black, USR_Production_PV_L);
                    TaskAPI["Production_PV"] = "";
                    break;

                case "USR_LAM_Run_Price_C":
                    SetForeColor(USR_LAM_Run_Price_C.Checked ? Color.Blue : Color.Black, USR_LAM_Run_Price_L);
                    TaskAPI["LAM_Run_Price"] = "";
                    break;

                case "USR_Run_ICD_Price_C":
                    SetForeColor(USR_Run_ICD_Price_C.Checked ? Color.Blue : Color.Black, USR_Run_ICD_Price_L);
                    TaskAPI["Run_ICD_Price"] = "";
                    break;
                case "USR_Marketing_Add_Data_C":
                    SetForeColor(USR_Marketing_Add_Data_C.Checked ? Color.Blue : Color.Black, USR_Marketing_Add_Data_L);
                    TaskAPI["Marketing_Add_Data"] = "";
                    break;
                case "USR_Purchasing_Add_Data_C":
                    SetForeColor(USR_Purchasing_Add_Data_C.Checked ? Color.Blue : Color.Black, USR_Purchasing_Add_Data_L);
                    TaskAPI["Purchasing_Add_Data"] = "";
                    break;
                case "USR_Logistic_Add_Data_C":
                    SetForeColor(USR_Logistic_Add_Data_C.Checked ? Color.Blue : Color.Black, USR_Logistic_Add_Data_L);
                    TaskAPI["Logistic_Add_Data"] = "";
                    break;
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!IsUsed)
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_Finance_Run_Cost_L, USR_Finance_Run_Cost.Text, "User: Finance Run Cost 必填");
            akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_Production_PV_L, USR_Production_PV.Text, "User: Production PV 必填");
            akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_LAM_Run_Price_L, USR_LAM_Run_Price.Text, "User: LAM Run Price 必填");
            akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_Run_ICD_Price_L, USR_Run_ICD_Price.Text, "User: Run ICD Price 必填");
            
            vald_s = Validation(regs);
            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>User Choose</b></font>");
            }

            return vald_s;
        }
        #endregion

    }
}
